<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>编辑地址</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="description" content="Write an awesome description for your new site here. You can edit this line in _config.yml. It will appear in your document head meta (for Google search results) and in your feed.xml site description.
">
    <link rel="stylesheet" th:href="@{/wxui/lib/weui.min.css}">
    <link rel="stylesheet" th:href="@{/wxui/css/jquery-weui.css}">
    <link rel="stylesheet" th:href="@{/wxui/css/style.css}">
</head>
<body ontouchstart>
<!--主体-->
<header class="wy-header">
    <div class="wy-header-icon-back"><span></span></div>
    <div class="wy-header-title">编辑地址</div>
</header>
<div class="weui-content">
    <form id="addressForm">
        <div class="weui-cells weui-cells_form wy-address-edit">
            <input type="hidden" name="id" id="addressId" th:value="${address==null? null:address.id}"/>
            <div class="weui-cell">
                <div class="weui-cell__hd"><label class="weui-label wy-lab">收货人</label></div>
                <div class="weui-cell__bd"><input class="weui-input"
                                                  th:value="${address==null? null:address.receivePersonName}"
                                                  name="receivePersonName"
                                                  placeholder="请填写收货人"></div>
            </div>
            <div class="weui-cell">
                <div class="weui-cell__hd"><label class="weui-label wy-lab">手机号</label></div>
                <div class="weui-cell__bd"><input class="weui-input"
                                                  th:value="${address==null? null:address.receivePersonPhone}"
                                                  name="receivePersonPhone"
                                                 placeholder="请填写手机号"></div>
            </div>
            <div class="weui-cell">
                <div class="weui-cell__hd"><label class="weui-label wy-lab">所在地区</label></div>
                <div class="weui-cell__bd"><input class="weui-input" id="address" type="text" value="湖北省 武汉市 武昌区"
                                                  readonly="" data-code="420106" data-codes="450000,420100,420106">
                </div>
                <input type="hidden" th:value="${address==null? '湖北省':address.province}" name="province" id="province"/>
                <input type="hidden" th:value="${address==null? 450000:address.provinceCode}" name="provinceCode"
                       id="provinceCode"/>
                <input type="hidden" th:value="${address==null? '武汉市':address.city}" name="city" id="city"/>
                <input type="hidden" th:value="${address==null? 420100:address.cityCode}" name="cityCode" id="cityCode"/>
                <input type="hidden" th:value="${address==null? '武昌区':address.area}" name="area" id="area"/>
                <input type="hidden" th:value="${address==null? 420106:address.areaCode}" name="areaCode" id="areaCode"/>
            </div>
            <div class="weui-cell">
                <div class="weui-cell__hd"><label class="weui-label wy-lab">详细地址</label></div>
                <div class="weui-cell__bd" style="margin-top: 15px;">
                    <textarea class="weui-textarea" name="address" th:text="${address==null? null:address.address}"
                              placeholder="请填写详细地址"></textarea>
                </div>
            </div>
            <div class="weui-cell weui-cell_switch">
                <div class="weui-cell__bd">设为默认地址</div><!--th:checked="${xx==1?true:false}"-->
                <div class="weui-cell__ft"><input class="weui-switch" type="checkbox" id="button1"></div>
                <input hidden="hidden"  name="isDefault" id="isDefault" type="radio"
                       th:value="${address==null? 0:address.isDefault}" checked="checked"/>
            </div>
        </div>
        <div class="weui-btn-area">
            <a class="weui-btn weui-btn_primary" href="javascript:" id="subAddress">保存此地址</a>
            <a href="javascript:;" class="weui-btn weui-btn_warn" id="delAddress"
               th:text="${address==null?null:'删除此地址'}"></a>
        </div>
    </form>
</div>
<script th:src="@{/wxui/lib/jquery-2.1.4.js}"></script>
<script th:src="@{/wxui/lib/fastclick.js}"></script>
<script type="text/javascript" th:src="@{/wxui/js/jquery.Spinner.js}"></script>
<script th:src="@{/wxui/js/jquery-weui.js}"></script>
<script th:src="@{/wxui/js/city-picker.js}"></script>
<script>
    $(function () {

        var isDefault = [[${address==null?0:address.isDefault}]];
        if (isDefault == 1) {
            $("#button1").attr("checked", true);
        }

        FastClick.attach(document.body);
        $(".wy-header-icon-back").click(function () {
            location.href='/receiveAddress/frontStage/goAddressListPage'
        });


    });
</script>

<script>

    /**
     * 地址选择器
     */
    $("#address").cityPicker({
        title: "选择所在地",
        onChange: function (picker, values, displayValues) {
            console.log(values, displayValues);
            $("#province").val(values[0]);
            $("#provinceCode").val(displayValues[0]);
            $("#city").val(values[1]);
            $("#cityCode").val(displayValues[1]);
            $("#area").val(values[2]);
            $("#areaCode").val(displayValues[2]);
        }
    });

    /**
     * 添加地址
     */
    $("#subAddress").click(function () {
        $.ajax({
            url: "/receiveAddress/frontStage/addOrUpdate",
            dataType: "json",
            data: $("#addressForm").serialize(),
            type: "post",
            success: function (data) {
                if (data.code == 200) {
                    $.toast(data.msg),
                        setTimeout('window.location.href=document.referrer', 1000)
                } else {
                    alert(data.msg);
                    $.toast(data.msg)
                }
            }
        })
    })

    /**
     * 删除地址
     */
    $("#delAddress").click(function () {
        $.ajax({
            url: "/receiveAddress/frontStage/deleteAddress",
            dataType: "json",
            data:{
                id:$("#addressId").val(),
                isDefault:$("#isDefault").val()
            },
            type: "post",
            success: function (data) {
                if (data.code == 200) {
                    $.toast(data.msg),
                        setTimeout('window.location.href=document.referrer', 1000)
                } else {
                    $.toast(data.msg)
                }
            }
        })
    })


    $("#button1").click(function () {
        if ($("#isDefault").val() == "0") {
            $("#isDefault").val("1");
        } else {
            $("#isDefault").val("0");
        }
    })
</script>
</body>
</html>
